'''
专项整治功能测试
'''
from Lib.base.page_object.special.special_rectification_list import SpecialRectificationList
from Lib.base.page_object.special.special_rectification_add import SpecialRectificationAdd
from Lib.base.page_object.special.special_task import SpecialTask
from selenium.webdriver.common.by import By
import allure
import os
from Lib.common.assert_class import AssertClass
import pytest
from Lib.common.get_data_by_mysql import MysqlAssert


@pytest.fixture(scope='class', autouse=True)
@pytest.mark.usefixtures('login')
def set_cls(login):
    TestSpecialRectification.special_rectification_list = SpecialRectificationList(login)
    TestSpecialRectification.special_rectification_add = SpecialRectificationAdd(login)
    TestSpecialRectification.special_rectification_task = SpecialTask(login)
    TestSpecialRectification.assert_class = AssertClass(login)


@allure.feature('专项整治页面测试')
class TestSpecialRectification:

    @classmethod
    def teardown_class(cls):
        # 删除测试数据
        sql = 'DELETE t FROM info_renovation t WHERE t.title LIKE "%UI%"'
        MysqlAssert().delete_data_by_sql(sql)

    @allure.title('新增专项整治')
    def test_special_rectification_add_to_draft(self):
        '''
        新增专项整治
        :return:
        '''
        self.special_rectification_add.add_special_rectification()
        # 设置断言
        assert_arg = (By.XPATH, '//p[@title = "UI新建的专项整治标题"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='UI新建的专项整治标题')

    @allure.title('专项整治筛选功能测试')
    def test_special_rectification_filter(self):
        '''
        筛选相关功能测试
        :return:
        '''
        with allure.step('根据筛选框进行筛选'):
            self.special_rectification_list.special_rectification_filter()
            # 设置断言  返回的列表内容全是方案制定
            assert_args = (By.XPATH, '//p[text()="方案制定"]')
            self.assert_class.make_assert_by_elements_text(args=assert_args, assert_data='方案制定')
        with allure.step('根据关键字进行搜索'):
            self.special_rectification_list.special_rectification_search()
            # 设置断言
            assert_arg = (By.XPATH, '//p[@title = "UI新建的专项整治标题"]')
            self.assert_class.make_assert_by_text(args=assert_arg, assert_data='UI新建的专项整治标题')

    @allure.title('专项整治收藏和取消收藏功能测试')
    def test_special_collect_and_cancel(self):
        '''
        筛选相关功能测试
        :return:
        '''
        with allure.step('收藏专项整治'):
            self.special_rectification_list.special_rectification_collect()
            # 设置断言  收藏列表当中包含
            assert_arg = (By.XPATH, '//p[@title = "UI新建的专项整治标题"]')
            self.assert_class.make_assert_by_text(args=assert_arg, assert_data='UI新建的专项整治标题')
        with allure.step('取消收藏专项整治'):
            self.special_rectification_list.special_rectification_cancel_collect()
            # 设置断言  收藏列表当中不包含
            assert_arg = (By.XPATH, '//p[@title = "UI新建的专项整治标题"]')
            self.assert_class.make_assert_by_elements_no_exist(args=assert_arg, assert_data='暂无数据')

    @allure.title('查看专项整治详情，并进行编辑')
    def test_special_rectification_detail_and_update(self):
        '''
        查看专项整治详情，并进行编辑
        :return:
        '''
        self.special_rectification_list.special_detail_and_update()
        # 设置断言
        assert_arg = (By.XPATH, '//p[@title = "UI编辑后的专项整治标题"]')
        self.assert_class.make_assert_by_text(args=assert_arg, assert_data='UI编辑后的专项整治标题')

    @allure.title('添加工作日志')
    def test_special_rectification_add_work_log(self):
        '''
        添加工作日志
        :return:
        '''
        self.special_rectification_list.special_rectification_add_work_log()
        # 设置断言
        assert_args = (By.XPATH, '//p[contains(text(), "暂无数据")]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='暂无数据', context='日志内容元素')

    @allure.title('专项整治流转流程')
    def test_special_rectification_detail_flow(self):
        '''
        专项整治流转流程
        :return:
        '''
        self.special_rectification_list.special_rectification_detail_flow()
        # 设置断言
        assert_args = (By.XPATH, '//p[text()="流转成功"]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='流转成功', context='流转完成')

    @allure.title('新增专项整治执法任务')
    def test_special_rectification_task_create(self):
        '''
        新增专项整治执法任务
        :return:
        '''
        self.special_rectification_task.special_task_create()
        # 设置断言
        assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='已下达')

    @allure.title('专项整治执法任务流程处置')
    def test_special_rectification_task_deal(self):
        '''
        专项整治执法任务流程处置
        :return:
        '''
        self.special_rectification_task.special_task_deal()
        # 设置断言
        assert_args = (By.XPATH, '//div[@title="已归档"]//div[text()="已归档"]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='已归档')

    @allure.title('专项整治执法任务退单操作')
    def test_special_rectification_task_chargeback(self):
        '''
        专项整治执法任务流程处置
        :return:
        '''
        with allure.step('新建执法任务'):
            self.special_rectification_task.special_task_create()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已下达')
        with allure.step('执法任务退单'):
            self.special_rectification_task.special_task_chargeback()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已退单"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='已退单')

    @allure.title('专项整治执法任务列表查询')
    def test_special_rectification_task_list(self):
        '''
        专项整治执法任务流程处置
        :return:
        '''
        self.special_rectification_task.special_task_list()
        # 设置断言
        assert_args = (By.XPATH, '//h2')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='UI编辑后的专项整治标题')

    @allure.title('专项整治草稿箱操作')
    def test_special_rectification_special_draft(self):
        '''
        专项整治流转流程
        :return:
        '''
        with allure.step('保存专项整治到草稿箱'):
            self.special_rectification_add.add_special_draft()
            # 设置断言
            assert_args = (By.XPATH, '//p[@title="UI新建的专项整治草稿"]')
            self.assert_class.make_assert_by_text(args=assert_args, assert_data='UI新建的专项整治草稿')
        with allure.step('删除草稿箱当中的专项整治'):
            self.special_rectification_list.delete_special_draft()
            # 设置断言
            assert_args = (By.XPATH, '//p[@title="UI新建的专项整治草稿"]')
            self.assert_class.make_assert_by_elements_no_exist(args=assert_args, assert_data='删除草稿箱')


if __name__ == '__main__':
    pytest.main(
        ['-v', '-s',
         './special_rectification_test.py::TestSpecialRectification'])
    # pytest.main(['-v', './special_rectification_test.py', '--alluredir', '../result', '--clean-alluredir'])
    # os.system(r'copy ..\config\environment.properties ..\result\environment.properties')
    # os.system('allure generate  ../result -o ../report/ --clean')
